package com.battleship.activity;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.graphics.Point;
import android.os.Bundle;
import android.view.MotionEvent;

import com.battleship.modelo.Barco;
import com.battleship.utils.Constants;
import com.battleship.utils.ScreenUtils;
import com.battleship.view.LocalGameSelectView;

public class Game extends Activity {
	
	private int tableX = 6;
	private int tableY = 1;
	
	// Variables para el movimiento
	private float pushX = 0;
	private float pushY = 0;
	private Barco pushedShip = null;
	private int touchCount = 0;
	LocalGameSelectView localGameSelectView;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScreenUtils.setFullScreen(this);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        /*
        int top = 0;
        for(int i = 0; i < tableY; i++){
        	int left = 0;
        	for(int j = 0; j < tableX; j++){
        		Button button = new Button(this);
        		button.setBackgroundColor(0);
        		button.setMaxHeight(10);
        		button.setMaxWidth(10);
        		button.setPadding(left, top, 0, 0);
        		setContentView(button);
        		left += 10;
        	}
        	top += 10;
        }*/
//        setContentView(R.layout.game);
        this.localGameSelectView = new LocalGameSelectView(this);
        setContentView(this.localGameSelectView);
	}
	@Override
	public boolean onTouchEvent(MotionEvent event) {

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			Object pushedObject = this.localGameSelectView.getPositionObject((int)event.getX(), (int)event.getY());
			if(pushedObject instanceof Barco) {
				if(touchCount > 0 && pushedObject == pushedShip) {
					pushedShip.rotate();
					this.localGameSelectView.invalidate();
				} else {
					pushX =  event.getX();
					pushY = event.getY();
					pushedShip = (Barco)pushedObject;
				}
				return true;
			} else {
				this.pushedShip = null;
			}
			break;
		case MotionEvent.ACTION_MOVE:
			if(pushedShip != null) {
				float newPushX = event.getX();
				float newPushY = event.getY();
				pushedShip.move((int)(newPushX-pushX), (int)(newPushY-pushY));
				pushX = newPushX;
				pushY = newPushY;
				this.localGameSelectView.invalidate();
				return true;
			}
			break;
		case MotionEvent.ACTION_UP:
			touchCount++;
			if(pushedShip!=null) {
				int moduleTop = (pushedShip.getBoundTop()-(Constants.BOX_SIZE/2)) % Constants.BOX_SIZE;
				int moduleLeft = pushedShip.getBoundLeft() % Constants.BOX_SIZE;
				if(moduleTop != 0) {
					if(moduleTop < 7){
						pushedShip.setBoundTop(pushedShip.getBoundTop()-moduleTop);
						pushedShip.setBoundBottom(pushedShip.getBoundBottom()-moduleTop);
					} else {
						pushedShip.setBoundTop(pushedShip.getBoundTop()+ ((Constants.BOX_SIZE) - moduleTop));
						pushedShip.setBoundBottom(pushedShip.getBoundBottom()+ ((Constants.BOX_SIZE) - moduleTop));
					}
				}
				if(moduleLeft != 0) {
					if(moduleLeft < 7) {
						pushedShip.setBoundLeft(pushedShip.getBoundLeft() - moduleLeft);
						pushedShip.setBoundRight(pushedShip.getBoundRight() - moduleLeft);
					} else {
						pushedShip.setBoundLeft(pushedShip.getBoundLeft()+ (Constants.BOX_SIZE - moduleLeft));
						pushedShip.setBoundRight(pushedShip.getBoundRight()+ (Constants.BOX_SIZE - moduleLeft));
					}
				}
				this.localGameSelectView.invalidate();
			}
			new Thread() {
				public void run() {
					try {
						Thread.sleep(300);
					} catch (InterruptedException e) {
					}
					touchCount = 0;
						
				}
			}.start();
			return true;
		}
		return false;
	}
}
